﻿/* ============================================================================== 
* 功能描述：BasicsHelper 
* 创 建 者：张教主 
* 当前版本: V1.0
* 创建日期：2019/2/20 15:17:15 
* ==============================================================================*/

using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Web;
using System.Web.Caching;

namespace src.Helper
{
    internal class BasicsHelper
    {
        #region get access_token
        /// <summary>
        /// get access_token
        /// </summary>
        /// <param name="appid"></param>
        /// <param name="secret"></param>
        /// <returns></returns>
        public static string GetAccessToken(string appid, string secret)
        {
            try
            {
                string accessToken = string.Empty;
                if (HttpContext.Current == null)
                {
                    accessToken = GetTokenOnLine(appid, secret);
                    return accessToken;
                }
                var accessTokenCache = HttpContext.Current.Cache["access_token"];
                if (accessTokenCache != null)
                {
                    accessToken = accessTokenCache.ToString();
                    return accessToken;
                }
                accessToken = GetTokenOnLine(appid, secret);
                return accessToken;

            }
            catch (System.Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// get token online
        /// </summary>
        /// <returns></returns>
        public static string GetTokenOnLine(string appid, string secret)
        {
            string url = WCDevUrl.GetAccessTokenUrl("client_credential", appid, secret);
            HttpHelper httpHelper = new HttpHelper();
            string jsonResponse = httpHelper.GetStr(url);
            if (string.IsNullOrWhiteSpace(jsonResponse))
            {
                throw new HttpException("please check you netWork");
            }
            JObject jObject = (JObject)JsonConvert.DeserializeObject(jsonResponse);
            if (jObject["access_token"] != null)
            {
                string accessToken = jObject["access_token"].ToString();
                HttpContext.Current.Cache.Insert("access_token", accessToken, null, DateTime.Now.AddMinutes(115), TimeSpan.Zero, CacheItemPriority.Normal, null);
                return accessToken;
            }
            throw new Exception(jObject.ToString());
        }
        #endregion

        #region get server ips
        /// <summary>
        /// get server ips
        /// </summary>
        /// <param name="accessToken"></param>
        /// <returns></returns>
        public static string GetWeChatServerIps(string accessToken)
        {
            HttpHelper httpHelper = new HttpHelper();
            string getServerIpUrl = WCDevUrl.GetWCServerIp(accessToken);
            string jsonResponse = httpHelper.GetStr(getServerIpUrl);
            JObject jObject = (JObject)JsonConvert.DeserializeObject(jsonResponse);
            if (jObject["ip_list"] != null)
            {
                return jObject["ip_list"].ToString();
            }
            throw new Exception(jObject.ToString());
        } 
        #endregion

        /// <summary>
        /// get ping info
        /// </summary>
        /// <param name="accessToken"></param>
        /// <param name="action"></param>
        /// <param name="check_operator"></param>
        /// <returns></returns>
        public static string GetPingInfo(string accessToken,string action="all",string check_operator= "DEFAULT")
        {
            string netCheckUrl = WCDevUrl.GetNetCheckUrl(accessToken);
            HttpHelper httpHelper = new HttpHelper();
            Dictionary<string, string> dict = new Dictionary<string, string>();
            dict.Add("action", action);
            dict.Add("check_operator", check_operator);
            string postStr = JsonConvert.SerializeObject(dict);
            string str = httpHelper.PostStr(postStr, netCheckUrl);
            return str;

        }

    }
}
